package com.xiaoyu.lanling.media.selector

import android.content.Intent
import com.xiaoyu.base.app.AppContext
import com.xiaoyu.lanling.media.selector.MediaSelectorFromType.MediaSelectorFromTypeDef
import com.xiaoyu.lanling.media.selector.MediaSelectorType.MediaSelectorTypeDef
import com.xiaoyu.lanling.media.selector.matisse.MediaMatisseActivity

class MediaSelector private constructor() {
    @MediaSelectorTypeDef
    var mediaType: String? = MediaSelectorType.IMAGE
        private set

    var maxCount = 1
        private set

    var withCamera = false
        private set

    var isWithCrop = false
        private set

    var withCropAspectRatioX = 1
        private set

    var withCropAspectRatioY = 1
        private set

    var original = false
        private set

    @MediaSelectorFromTypeDef
    var fromType = 0
        private set

    var maxVideoDuration: Long = 15000
        private set

    var videoIncapableMessage: String = ""

    @JvmOverloads
    fun selectorForImage(maxCount: Int, withCamera: Boolean, @MediaSelectorFromTypeDef fromType: Int, original: Boolean = false) {
        mediaType = MediaSelectorType.IMAGE
        this.maxCount = maxCount
        this.withCamera = withCamera
        this.fromType = fromType
        this.original = original
        goToMediaMatisse()
    }

    fun selectorForImageWithGif(maxCount: Int, withCamera: Boolean, @MediaSelectorFromTypeDef fromType: Int, original: Boolean = false) {
        mediaType = MediaSelectorType.IMAGE_WITH_GIF
        this.maxCount = maxCount
        this.withCamera = withCamera
        this.fromType = fromType
        this.original = original
        goToMediaMatisse()
    }

    fun selectorForTinyVideo(maxCount: Int, maxDurationInMills: Long, @MediaSelectorFromTypeDef fromType: Int, incapableCauseMsg: String = "") {
        mediaType = MediaSelectorType.TINY_VIDEO
        this.maxVideoDuration = maxDurationInMills
        this.maxCount = maxCount
        this.fromType = fromType
        this.videoIncapableMessage = incapableCauseMsg
        goToMediaMatisse()
    }

    fun selectorForAll(maxCount: Int, @MediaSelectorFromTypeDef fromType: Int, original: Boolean = false) {
        mediaType = MediaSelectorType.ALL
        this.maxCount = maxCount
        this.fromType = fromType
        this.original = original
        goToMediaMatisse()
    }

    @JvmOverloads
    fun selectorForImageWithCrop(withCamera: Boolean, @MediaSelectorFromTypeDef fromType: Int, original: Boolean = false) {
        selectorForImageWithCrop(withCamera, withCropAspectRatioX, withCropAspectRatioY, fromType, original)
    }

    @JvmOverloads
    fun selectorForImageWithCrop(withCamera: Boolean, withCropAspectRatioX: Int, withCropAspectRatioY: Int,
                                 @MediaSelectorFromTypeDef fromType: Int, original: Boolean = false) {
        isWithCrop = true
        this.withCropAspectRatioX = withCropAspectRatioX
        this.withCropAspectRatioY = withCropAspectRatioY
        selectorForImage(1, withCamera, fromType, original)
    }

    @JvmOverloads
    fun selectorForTinyVideo(withCamera: Boolean, maxDuration: Long, message: String) {
        this.maxVideoDuration = maxDuration
        this.videoIncapableMessage = message
        this.mediaType = MediaSelectorType.TINY_VIDEO
        goToMediaMatisse()
    }

    @JvmOverloads
    fun selectorForTinyVideoOnly(withCamera: Boolean, maxDuration: Long, message: String) {
        this.maxVideoDuration = maxDuration
        this.videoIncapableMessage = message
        this.mediaType = MediaSelectorType.TINY_VIDEO_ONLY
        goToMediaMatisse()
    }

    fun reset() {
        maxCount = 1
        withCamera = false
        original = false
        isWithCrop = false
        withCropAspectRatioX = 1
        withCropAspectRatioY = 1
    }

    private fun goToMediaMatisse() {
        val context = AppContext.getContext()
        val intent = Intent(context, MediaMatisseActivity::class.java)
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        context.startActivity(intent)
    }

    companion object {
        val instance by lazy { MediaSelector() }
        const val REQUEST_CODE_CHOOSE = 24
    }
}